<?php 
	include("library/vars.php");
	
	if(isset($_POST['submit'])) { // If the form is submited
	
		$season_name = $_POST['season_name'];
	
		include("library/config.php");
		include("library/opendb.php");
	
		// Check to see if there's already a season with that number
		$query="SELECT * FROM seasons WHERE season='$season_name' AND show_ID='$show'";
		$result = mysql_query($query);
		$num=mysql_numrows($result);
	
		if ($num==0) { // If there's no season with that number
			$query2 = "INSERT INTO seasons (show_ID, season) VALUES ('$show', '$season_name')";
			mysql_query($query2) or die('Database Error');
			
			if (isset($_POST['import'])){ 
				// import episodes
				$getshow = $show;
				
				include("library/parse_fix.php");
				include("library/getting_episodes.php");
				
					if ($number > 0) {
				
						foreach($arrayShows as $value) {

				    		$episodenumber = trim(substr($value['episode'],-2, 2), " ");
							$value['url'] = str_replace(" &amp", "&", $value['url']);
							$value['url'] = str_replace('<a target="_blank" href="', '', $value['url']);
							$value['url'] = str_replace('<a target="visit" href="', '', $value['url']);
							$value['url'] = str_replace('">', '', $value['url']);
							$url = $value['url'];
				   			$value['title'] = str_replace(" &amp", "&", $value['title']);
							$value['title'] = str_replace("</a>", "", $value['title']);
							$title = addslashes($value['title']);
							
							include("library/get_summary.php");
							$summary = addslashes($summary);
							

				    		$query3 = "INSERT INTO episodes (show_ID, season_ID, episode, title, description) VALUES ('$show', '$season_name', '$episodenumber', '$title', '$summary')";
							mysql_query($query3) or die('Database Error');

						}
						
					}
					else {
	
					};
				
			} 

			include 'library/closedb.php';
			header( 'Location: index.php?s=' . $show . '&t=' . $season_name . '&m=season_added' );
		} 
		else {
			include 'library/closedb.php';
			header( 'Location: add_season.php?s=' . $show . '&t=' . $season_name . '&m=season_already_exists' );
		}
	}
	else {
		
			include("library/config.php");
			include("library/opendb.php");

			$query="SELECT id, show_name, show_nicename FROM shows WHERE show_nicename='$show'";
			$result = mysql_query($query);

			$i=0;
			$showname=mysql_result($result,$i,"show_name");

			include("library/closedb.php");
			include("library/header.php");
?>
	
<div id="main">
	<div id="seasons">
		<h2><?php echo _("Add New Season to"); ?>: <strong><?=$showname?></strong></h2>
	</div>
	
	<div id="messages">
		<?php include ("library/messages.php");	?>
	</div>
	
	<form method="post" autocomplete="off" id="add" action="">
		<p><label for="season_name"><?php echo _("Season Number"); ?>:</label> <input name="season_name" type="text" id="season_name" style="width:25px" class="{required:true,digits:true}" value="<?=$season;?>" /></p>
		
		<p><input type="checkbox" name="import" id="import" checked="checked" style="width:auto;margin-left:130px" /><label for="import" style="float:none;"><?php echo _("Automatically import all episodes for this season from"); ?> <img src="library/images/epguides.png" alt="epguides.com" class="img_logo" /> &amp; <img src="library/images/tv.gif" alt="TV.com" class="img_logo" /></label></p>
 		
		<p class="submit"><input type="submit" name="submit" class="submit" id="submit" value="<?php echo _("Add New Season"); ?>" /> <?php echo _("or"); ?> <a href="index.php?s=<?php echo $show ?>"><?php echo _("Cancel"); ?></a></p>
	</form>
	
	<div id="import_notice">
		<p><?php echo _("<strong>Importing episodes...</strong><br />This might take a while."); ?></p>
		<p class="nota"><?php echo _("Please don't close this window"); ?>.</p>
	</div>
	
	<div class="push"></div>

</div><!-- /#main -->
<?php include("library/footer.php");?>
</body>
</html>
<?php } ?>